package main

import (
    pb "example.com/http-grpc-server/proto/hello" // 引入编译生成的包
    "golang.org/x/net/context"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    "log"
)

const (
    // Address gRPC服务地址
    Address = "127.0.0.1:50052"
)

/**
* @Description: 普通grpc客户端
* @Author: liangan
* @Date: 2023-09-02_10:18
*/
func main() {
    // 连接
    conn, err := grpc.Dial(Address, grpc.WithTransportCredentials(insecure.NewCredentials()))
    if err != nil {
        log.Println("连接失败: %v", err)
    }
    defer conn.Close()

    // 初始化客户端
    c := pb.NewHelloClient(conn)

    // 调用方法
    log.Println("通过gRPC调用SayHello方法")
    req := &pb.HelloRequest{Name: "gRPC"}
    res, err := c.SayHello(context.Background(), req)

    if err != nil {
        log.Fatalf("请求失败: %v", err)
    }
    log.Printf("请求结果: %s", res.GetMessage())

}

